package com.p595263720.o_amap.search;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.google.gson.Gson;
import com.p595263720.o_amap.search.model.UnifiedGeocodeResult;
import com.p595263720.o_amap.search.model.UnifiedInputTipsResult;
import com.p595263720.o_amap.search.model.UnifiedPoiResult;
import com.p595263720.o_amap.search.model.UnifiedPoiSearchQuery;
import com.p595263720.o_amap.search.model.UnifiedSearchBound;

import java.util.List;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class SearchHandlers implements MethodChannel.MethodCallHandler, PoiSearch.OnPoiSearchListener, GeocodeSearch.OnGeocodeSearchListener, Inputtips.InputtipsListener {
    private Gson gson;
    private MethodChannel.Result result;
    private Context mContext;

    public SearchHandlers(Context context) {
        mContext = context;
        gson = new Gson();
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        this.result = result;
        switch (call.method) {
            case "searchPoi":
                searchPoiBound(call);
            case "searchPoiBound":
                searchPoiBound(call);
                break;
            case "searchReGeocode":
                searchReGeocode(call);
                break;
            case "searchGeocode":
                searchGeocode(call);
                break;
            case "inputTips":
                inputTips(call);
                break;
        }
    }


    //周边搜索
    public void searchPoiBound(MethodCall call) {
        String query = call.argument("query") != null ? (String) call.argument("query") : "{}";
        UnifiedPoiSearchQuery searchQuery = gson.fromJson(query, UnifiedPoiSearchQuery.class);
        PoiSearch poiSearch = searchQuery.toPoiSearchBound(mContext);
        poiSearch.searchPOIAsyn();
        poiSearch.setOnPoiSearchListener(this);
    }


    public void searchReGeocode(MethodCall call) {
        String point = call.argument("point");
        LatLonPoint latLonPoint = UnifiedSearchBound.toLatLonPoint(gson.fromJson(point, LatLng.class));
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 100, GeocodeSearch.AMAP);
        GeocodeSearch geocodeSearch = new GeocodeSearch(mContext);
        geocodeSearch.getFromLocationAsyn(query);
        geocodeSearch.setOnGeocodeSearchListener(this);
    }

    public void searchGeocode(MethodCall call) {
        String cityName = call.argument("city");
        String name = call.argument("name");
        GeocodeSearch geocodeSearch = new GeocodeSearch(mContext);
        GeocodeQuery geocodeQuery = new GeocodeQuery(name, cityName);
        geocodeSearch.getFromLocationNameAsyn(geocodeQuery);
        geocodeSearch.setOnGeocodeSearchListener(this);
    }

    //POI搜索监听
    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        try {
            if (i == AMapException.CODE_AMAP_SUCCESS) {
                if (poiResult != null) {
                    this.result.success(gson.toJson(new UnifiedPoiResult(poiResult)));
                } else {
                    this.result.error(i + "", "未搜索到结果", "未搜索到结果");
                }
            } else {
                this.result.error(i + "", "未搜索到结果", "未搜索到结果");
            }

        } catch (Exception e) {
        }

    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    //地理逆编码结果监听
    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        try {
            if (regeocodeResult != null) {
                this.result.success(JSON.toJSONString(regeocodeResult));
            } else {
                this.result.error(i + "", "未搜索到结果", "未搜索到结果");
            }
        } catch (Exception e) {
        }

    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        try {
            if (geocodeResult != null) {
                this.result.success(gson.toJson(new UnifiedGeocodeResult(geocodeResult)));
            } else {
                this.result.error(i + "", "未搜索到结果", "未搜索到结果");
            }
        } catch (Exception e) {
        }

    }


    public void inputTips(MethodCall call) {
        String cityName = call.argument("city");
        String keyword = call.argument("keyword");
        if (keyword != null && keyword.length() > 0) {
            InputtipsQuery inputtipsQuery = new InputtipsQuery(keyword, cityName);
            if (cityName != null && cityName.length() > 0) {
                inputtipsQuery.setCityLimit(true);
            }
            Inputtips inputtips = new Inputtips(mContext, inputtipsQuery);//定义一个输入提示对象，传入当前上下文和搜索对象
            inputtips.setInputtipsListener(this);//设置输入提示查询的监听，实现输入提示的监听方法onGetInputtips()
            inputtips.requestInputtipsAsyn();//输入查询提示的异步接口实现
        }
    }


    @Override
    public void onGetInputtips(List<Tip> list, int i) {
        try {
            if (list != null) {
                this.result.success(gson.toJson(new UnifiedInputTipsResult(list)));
            } else {
                this.result.error(i + "", "未搜索到结果", "未搜索到结果");
            }
        } catch (Exception e) {
        }
    }
}
